# from sanic import Sanic
# from sanic.response import json, html
# from sanic_cors import CORS
# import uvicorn
# import asyncio
# from det import result
# import os
#
#
# app = Sanic(__name__)
# # app.static('/static', './static')
# CORS(app)
#
#
# @app.get("/")
# async def index(request):
#     with open("templates/index.html", 'r', encoding='utf-8') as f:
#         return html(f.read())
#
#
# @app.post("/mask")
# async def upload(request):
#     try:
#         file = request.files.get('file')
#         # print(file.body)
#
#         path = os.path.join(os.getcwd(), 'data/images/1.jpg')
#         # print(path)
#         with open(path, 'ab+') as tp:
#             tp.write(file.body)
#
#         r = result()
#         return json(r)
#     except:
#         return json({"code": 0, "img": '', "advice": '', "cfdLevel": 0})
#
#
# if __name__ == "__main__":
#     uvicorn.run(app='manage:app', host="0.0.0.0", port=80, reload=True, workers=10)

from flask import Flask, render_template, request
from flask_cors import CORS
from detect1 import result
import os
from gevent import pywsgi

app = Flask(__name__)
CORS(app)


@app.route('/')
def hello_world():
    return render_template("index.html")


@app.post("/mask")
def upload():
    # try:
    file = request.files.get('file')
    # print(file.body)

    # path = os.path.join(os.getcwd(), 'data/images/1.jpg')
    # file.save(path)
    # print(path)
    # with open(path, 'ab+') as tp:
    #     tp.write(file.read())

    r = result(file.read())
    print(r["label"])
    return r
    # except:
    #     return {"code": 0, "img": '', "advice": '', "cfdLevel": 0}


s = pywsgi.WSGIServer(("0.0.0.0", 8000), app)
s.serve_forever()